package com.lancoo.ccas52.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lancoo.ccas52.entity.*;
import com.lancoo.ccas52.exception.BizException;
import com.lancoo.ccas52.mapper.RoomTypeMapper;
import com.lancoo.ccas52.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: liufeng
 * @CreateTime: 2024-07-08  17:18
 * @Description:
 */
@Service
public class RoomTypeServiceImpl extends ServiceImpl<RoomTypeMapper, RoomType> implements RoomTypeService {

    @Resource
    private TeachingClassWeekService teachingClassWeekService;
    @Resource
    private TeachingClassService teachingClassService;
    @Resource
    private RoomService roomService;
    @Resource
    private CourseWeekService courseWeekService;


    @Override
    public RoomType addRoomType(Integer taskId, String schoolId, String term, RoomType roomType) {
        Long count = this.lambdaQuery()
                .eq(RoomType::getSchoolId, schoolId)
                .eq(RoomType::getTerm, term)
                .eq(RoomType::getTypeName, roomType.getTypeName())
                .count();
        if (count != null && count > 0){
            throw new BizException(5,"类型名称重复");
        }
        this.save(roomType);
        return roomType;
    }

    @Override
    public void updateRoomType(Integer taskId, String schoolId, String term, RoomType roomType) {
        Long count = this.lambdaQuery()
                .eq(RoomType::getSchoolId, schoolId)
                .eq(RoomType::getTerm, term)
                .eq(RoomType::getTypeName, roomType.getTypeName())
                .count();
        if (count != null && count > 0){
            throw new BizException(5, "类型名称重复");
        }
        this.updateById(roomType);
    }

    @Override
    public void removeRoomType(Integer taskId, String schoolId, String term, Integer id) {
//        List<Room> roomList = roomService.lambdaQuery()
//                .eq(Room::getSchoolId, schoolId)
//                .eq(Room::getTerm, term)
//                .eq(Room::getRoomType, id)
//                .list();
//        List<CourseWeek> courseWeekList = courseWeekService.lambdaQuery()
//                .eq(CourseWeek::getTaskId, taskId)
//                .eq(CourseWeek::getRoomType, id)
//                .list();
//        List<Long> tcIds = teachingClassService.lambdaQuery()
//                .eq(TeachingClass::getTaskId, taskId)
//                .list().stream().map(TeachingClass::getTeachingClassId)
//                .distinct().collect(Collectors.toList());
//        List<TeachingClassWeek> teachingClassWeekList = new ArrayList<>();
//        if (CollUtil.isNotEmpty(tcIds)){
//            teachingClassWeekList = teachingClassWeekService.lambdaQuery()
//                    .eq(TeachingClassWeek::getTeachingClassId, tcIds)
//                    .eq(TeachingClassWeek::getRoomType, id)
//                    .list();
//        }
//        if (CollUtil.isNotEmpty(roomList) || CollUtil.isNotEmpty(courseWeekList)|| CollUtil.isNotEmpty(teachingClassWeekList)){
//            throw new BizException("该类型被使用，无法被删除");
//        }

        roomService.lambdaUpdate()
                .eq(Room::getSchoolId, schoolId)
                .eq(Room::getTerm, term)
                .eq(Room::getRoomType, id)
                .set(Room::getRoomType,null)
                .update();

        this.removeById(id);

    }

    @Override
    public List<RoomType> getRoomType(Integer taskId, String schoolId, String term, Integer type) {
        if (type != null && type == 1){
            return this.lambdaQuery()
                    .eq(RoomType::getSchoolId,schoolId)
                    .eq(RoomType::getTerm,term).list();
        }
        if (type == null){
            List<Integer> types = roomService.getRoomTypeUsed(schoolId,term);
            return this.lambdaQuery()
                    .eq(RoomType::getSchoolId,schoolId)
                    .eq(RoomType::getTerm,term)
                    .in(RoomType::getId,types)
                    .list();
        }
        return new ArrayList<>();
    }
}
